package com.tedu.egspringbootmvc.controller;

import com.tedu.egspringbootmvc.common.result.JsonResult;
import com.tedu.egspringbootmvc.common.result.StatusCode;
import com.tedu.egspringbootmvc.mapper.UserMapper;
import com.tedu.egspringbootmvc.pojo.dto.UserLoginDTO;
import com.tedu.egspringbootmvc.pojo.entity.User;
import com.tedu.egspringbootmvc.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/v1/users")
@Slf4j
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperation("登录功能")
    @PostMapping("/login")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO , @ApiIgnore HttpSession session){
        System.out.println(userLoginDTO);
        User user = userMapper.selectUser(userLoginDTO.getUsername(), userLoginDTO.getPassword());
        if (user!=null){
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            session.setAttribute("user",user);
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }else {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }
}
